package com.example.cardgame

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var  playerAvator: ImageView
    private lateinit var playerName: TextView
    private lateinit var playerGender: TextView
    private lateinit var playerPhone: TextView
    private lateinit var playerHighScore: TextView
    private lateinit var editAvator: Button
    private lateinit var playerStuno: TextView
    private lateinit var startGame:Button
    private lateinit var playerHobbies: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        playerAvator = findViewById(R.id.playerAvator)
        playerName = findViewById(R.id.playerName)
        playerGender = findViewById(R.id.playerGender)
        playerPhone = findViewById(R.id.playerPhone)
        playerHighScore = findViewById(R.id.playerHighScore)
        editAvator=findViewById(R.id.editAvator)
        playerStuno=findViewById(R.id.playerStuno)
        startGame=findViewById(R.id.startGame)
        playerHobbies = findViewById(R.id.playerHobbies)


//        修改头像
        val launcher2=registerForActivityResult(ActivityResultContracts.TakePicturePreview()){
            playerAvator.setImageBitmap(it)
        }
        editAvator.setOnClickListener{
            launcher2.launch(null)
        }

        startGame.setOnClickListener{
            val intent=Intent(this,FirstActivity::class.java)
            startActivityForResult(intent, 1)
        }

        val editInfoButton = findViewById<Button>(R.id.editInfoButton)

        editInfoButton.setOnClickListener {
            val intent = Intent(this, EditInfoActivity::class.java)
           startActivityForResult(intent, 2)
        }

    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode == Activity.RESULT_OK) {
            data?.let {
                when (requestCode) {
                    1 -> {
                        val score = it.getIntExtra("score", 0)
                        playerHighScore.text = "分数: $score"
                    }
                    2 -> {
                        playerName.text = "姓名：: ${it.getStringExtra("name")}"
                        playerGender.text = "性别：: ${it.getStringExtra("gender")}"
                        playerPhone.text = "电话: ${it.getStringExtra("phone")}"
                        playerStuno.text = "学号: ${it.getStringExtra("stuno")}"
                        val hobbies = it.getStringArrayListExtra("hobbies")
                        playerHobbies.text = "爱好: ${hobbies?.joinToString(", ")}"
                    }
                }
            }
        }
    }
}